package leadzone.actions.config;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import leadzone.persistence.bojo.config.system.ClusterParam;
import leadzone.persistence.dao.config.system.DataEnumDao;
import leadzone.persistence.dao.config.system.ModelParamDao;
import leadzone.present.BaseAction;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

@Scope("prototype")
@ParentPackage(value = "ajax-json-plugin")
@Component("EqptModelAction")
// @ParentPackage("json-default")
// @Result(name = "successJson", type = "json", params = {"name", "user",
// "ignoreHierarchy", "false"})
@Action(value = "eqptModel")
@Results({@Result(name = "success", location = "EquipmentDetail.jsp"),
		@Result(name = "error", location = "/error.jsp")})
public class EqptModelAction extends BaseAction {

	private String			eqptModelData;

	private String			eqptMeasureData;

	@Autowired
	private ModelParamDao	dao;
	@Autowired
	private DataEnumDao		dataEnumDao;

	@Override
	public String execute() {
		return SUCCESS;
	}
	
	/** 
	 * @Title: save 
	 * @Description: 保存设备分类，和设备量测更新值到对应的表中
	 * @param @return    设定文件 
	* @return String    返回类型 
	* @throws 
	 */
	public String save() {
		
		String jsonString = request.getParameter("jsondata");

		// {"extra":{"eqptid":111,}}

		JSONObject jsonobj = JSONObject.parseObject(jsonString);

		dao.parseDataJsonToDataBase(jsonobj, "eqptid", null);
		setMessage("操作成功！");
		setStatusCode(200);
		
		
		return SUCCESS;
	}
	/**
	 * @Title: getMeasure
	 * @Description: 获取设备量测，这杯类别在 extra
	 * @param 设定文件
	 * @return void 返回类型
	 * @throws
	 */
	public void getMeasure() {
		Map < String , String > extraList = new HashMap < String , String >();
		String feilds = request.getParameter("feilds");
		String extra = request.getParameter("extra");
		if ((feilds == null)) {
			response.setStatus(404);

			return;
		}
		if (extra != null) {
			JSONObject extrjson = JSONObject.parseObject(extra);

			Iterator < String > extrjsonIt = extrjson.keySet().iterator();

			while (extrjsonIt.hasNext()) {
				String field = extrjsonIt.next().toString();

				extraList.put(field, extrjson.getString(field));

			}
		}

		try {
			setResult(dataEnumDao.find(feilds, extraList));
			// 没有数据返回404 ，避免出现返回空字符串，出现空白行
			if (getResult().isEmpty()) {
				response.setStatus(404);
				return;
			}

			response.setStatus(200);
			response.getWriter().write(getResult());
			response.getWriter().close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @Title: getList
	 * @Description: 获取设备的详细分类列表
	 * @param 设定文件
	 * @return void 返回类型
	 * @throws
	 */
	public void getList() {
		String feilds = request.getParameter("feilds");
		String DataType = request.getParameter("DataType");
		if ((DataType == null)) {
			response.setStatus(404);

			return;
		}

		Map < String , String > extraList = new HashMap < String , String >();

		extraList.put("DataType", DataType);

		try {
			setResult(dao.find(feilds, null));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			// 没有数据返回404 ，避免出现返回空字符串，出现空白行
			if (getResult().isEmpty()) {
				response.setStatus(404);
				return;
			}

			response.setStatus(200);
			response.getWriter().write(getResult());
			response.getWriter().close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setEqptModelData(String eqptModelData) {
		this.eqptModelData = eqptModelData;
	}

	public void setEqptMeasureData(String eqptMeasureData) {
		this.eqptMeasureData = eqptMeasureData;
	}

}
